/**
 * @(#) ContractMediator.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	공지사항 - Mediator
 * 				공지사항 Presentation Logic을 관리 
 */
package com.skt.common_hr.search.view
{
	import com.skt.common_hr.common.Messages;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.search.SearchFacade;
	import com.skt.common_hr.search.events.SchSelMemberEvent;
	import com.skt.common_hr.search.events.SearchEvent;
	import com.skt.common_hr.search.model.JobProxy;
	import com.skt.common_hr.search.model.OrgProxy;
	import com.skt.common_hr.search.model.SearchProxy;
	import com.skt.common_hr.search.view.components.SearchView;
	
	import flash.events.Event;
	
	import mx.resources.ResourceManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class SearchMediator extends OverViewMediator
	{
		public static const NAME:String = "SearchMediator";
		private var schMsg:String = "";
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function SearchMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			preloadData();
 			super.initializeView();
 		}

 		override protected function activatedView():void
 		{
 			setEventListener();
			if(Variables.moduleParams!=null) {
				var proxy:SearchProxy = facade.retrieveProxy(SearchProxy.NAME) as SearchProxy;
				proxy.search(Variables.moduleParams["KEYWORD"]);
				view.txtKeyword.text = schMsg = Variables.moduleParams["KEYWORD"];
				Variables.moduleParams=null;
			}
			else {
				view.initView();
			}
 		}
 		
		override protected function closedView():void
		{
			removeEventListener();	
		}
		
		private function setEventListener():void
		{
 			view.addEventListener(SearchEvent.HR_SEARCH, 	onSearch);
 			view.addEventListener(SearchEvent.DETAIL_SEARCH, onDetailSearch);
 			view.addEventListener(SchSelMemberEvent.SCH_SELECTED_MEMBER, onSelectedMember);
 			view.addEventListener(SearchView.CHANGE_BASE_STATE, onChangaBaseState);
		}
		 
		private function removeEventListener():void
		{
 			view.removeEventListener(SearchEvent.HR_SEARCH, 	onSearch);
 			view.removeEventListener(SearchEvent.DETAIL_SEARCH, onDetailSearch);
 			view.removeEventListener(SchSelMemberEvent.SCH_SELECTED_MEMBER, onSelectedMember);
 			view.removeEventListener(SearchView.CHANGE_BASE_STATE, onChangaBaseState);
		}		
 		
 		private function preloadData():void
 		{
 			var jobProxy:JobProxy = facade.retrieveProxy(JobProxy.NAME) as JobProxy;
 			jobProxy.load();
 			var orgProxy:OrgProxy = facade.retrieveProxy(OrgProxy.NAME) as OrgProxy;
 			orgProxy.load(Variables.myPernr);
 		}
 		
 		private function onSearch(event:SearchEvent):void
 		{
			var proxy:SearchProxy = facade.retrieveProxy(SearchProxy.NAME) as SearchProxy;
			proxy.search(event.keyword);
			schMsg = event.keyword;
 		}
 		
  		private function onDetailSearch(event:SearchEvent):void
 		{
			var proxy:SearchProxy = facade.retrieveProxy(SearchProxy.NAME) as SearchProxy;
			var table:Array = new Array();
			table.push(event.condition);
			proxy.search(event.keyword, table);
			schMsg = event.keyword; 
 		}
 		
 		//회원 선택
 		private function onSelectedMember(event:SchSelMemberEvent):void
		{
			if(view.currentState == null) {
				view.currentState = "detail_view";
				facade.registerMediator(new SchProfileMediator(view.overView));
				view.tileMember.dataProvider = null;
			}
			facade.sendNotification(SearchFacade.CHANGE_SELECTED_MEMBER, event.pernr);
		}
		
		private function onChangaBaseState(event:Event):void
		{
			facade.removeMediator(SchProfileMediator.NAME);
			//view.changeLargeRenderer();
			view.tileMember.horizontalScrollPosition = 0;
			view.currentState = null;
		}
 		
	
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat( [
					Messages.SEND_PARAMETER,
					SearchFacade.LOAD_SEARCH_COMPLETE,
					SearchFacade.LOAD_SEARCH_FAILED,
					SearchFacade.LOAD_JOB_COMPLETE,
					SearchFacade.LOAD_JOB_FAILED,
					SearchFacade.LOAD_ORGLIST_COMPLETE,
					SearchFacade.LOAD_ORGLIST_FAILED
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case Messages.SEND_PARAMETER:
					trace("String(note.getBody()) : " + String(note.getBody()));
					view.txtKeyword.text = String(note.getBody());
					view.onClickSearch(view.txtKeyword.text);
					//trace("recieved parameter: " + String(note.getBody()));
					break;
				case SearchFacade.LOAD_SEARCH_COMPLETE:
					trace("SearchFacade.LOAD_SEARCH_COMPLETE");
					view.modeBasic();
					view.memberList = (note.getBody() as SearchProxy).searchResult;
					view.RESULT_MSG = buildResultMsg(schMsg, view.memberList.length);
					view.categoryTabCreate();
					view.filterMember(0);
					//setStatusMessage("검색이 완료되었습니다.");
					break;
				case SearchFacade.LOAD_SEARCH_FAILED:
					if((note.getBody() as SearchProxy).msgStr == ""){
						CommonAlert.showMessage(commonFacade.bundle, "ALERT_NO_SEARCH_DATA");
					}else{
						CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as SearchProxy).msgStr);
					}
					//setStatusMessage("FAILED : " + (note.getBody() as SearchProxy).msgStr);
					break;	
				case SearchFacade.LOAD_JOB_COMPLETE:
					view.jobList = (note.getBody() as JobProxy).jobList;
					view.categoryTabCreate();
					view.filterMember(0);
					//setStatusMessage("");
					break;
				case SearchFacade.LOAD_JOB_FAILED:
					//setStatusMessage("직무 리스트를 가져오는 데 실패했습니다.");
					break;
				case SearchFacade.LOAD_ORGLIST_COMPLETE:
					view.orgList = (note.getBody() as OrgProxy).orgList;
					break;
				case SearchFacade.LOAD_ORGLIST_FAILED:
					//setStatusMessage("부서 리스트를 가져오는 데 실패했습니다.");
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		private function buildResultMsg(keyword:String, length:Number):String
		{
			var msg:String = ResourceManager.getInstance().getString(commonFacade.bundle, "SEARCH_INFO_01")
			if(msg==null)
				return "";
			var param:Array = [keyword, length];
			while(msg.search("%p")>= 0) {
				var p:* = param.shift();
				msg = msg.replace("%p", (p==undefined)? "" : p);
			} 
			return msg;
		}
		
		
		
		
 
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():SearchView
        {
            return viewComponent as SearchView;
        }
	}
}